package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.RoadpoleDto;
import com.hualude.roadsign.dto.RoadsignDto;
import com.hualude.roadsign.repository.RoadPoleRepository;
import com.hualude.roadsign.repository.RoadSignRepository;
import com.hualude.roadsign.repository.UserActorRepository;
import com.hualude.roadsign.service.RoadSignAuditService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ding on 2017/4/9.
 */
@Service
public class RoadSignAuditServiceImpl implements RoadSignAuditService {
    @Resource
    private RoadPoleRepository roadPoleRepository;

    @Resource
    private RoadSignRepository roadSignRepository;


    public DTResponseDto getRoadPoleAudit4DT(DTRequestDto dtRequestDto, Integer orgId, String actorIds) throws Exception {
        List<RoadpoleDto> roadpoleDtos = roadPoleRepository.getRoadPoleAudit4DT(dtRequestDto, orgId, actorIds);
        Integer count = roadPoleRepository.getCount4RoadPoleAuditDT(dtRequestDto.getSearch(), orgId, actorIds);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, roadpoleDtos);
    }

    @Transactional
    public Integer auditRoadPole(Integer roadPoleIds, Integer flag) throws Exception {
        roadSignRepository.auditRoadSign(roadPoleIds, flag);
        return roadPoleRepository.auditRoadPole(roadPoleIds, flag);
    }

    public DTResponseDto getRoadSignAudit4DT(DTRequestDto dtRequestDto, Integer roadPoleId) throws Exception {
        List<RoadsignDto> roadsignDtos = roadSignRepository.getRoadSignAudit4DT(dtRequestDto, roadPoleId);
        Integer count = roadSignRepository.getRoadSignAuditCount4DT(roadPoleId);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, roadsignDtos);
    }

    @Transactional
    public Integer auditRoadPoleSign(String roadPoleIds) throws Exception {
        String[] roadPoleIdArray = roadPoleIds.split(",");
        Integer i;
        for (i = 0; i < roadPoleIdArray.length; i++) {
            Integer t = Integer.parseInt(roadPoleIdArray[i]);
            roadSignRepository.auditRoadSign(t, 1);
            roadPoleRepository.auditRoadPole(t, 1);
        }
        return 1;
    }

    @Transactional
    public Integer auditRoadSign(Integer roadSignId) throws Exception {
        return roadSignRepository.auditRoadSignById(roadSignId);
    }

    public Integer getRoadSignAuditCount(Integer orgId, Integer actorId) throws Exception {
        String actorIds = actorId.toString();
        return roadPoleRepository.getCount4RoadPoleAuditDT("", orgId, actorIds);
    }


}
